﻿using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Windows.Controls;
using Framework;
using System.ComponentModel.Composition;

namespace Demo
{
    public partial class MainPage : INotifyPropertyChanged
    {
        public MainPage()
        {
            InitializeComponent();
            CompositionInitializer.SatisfyImports(this);

            DataContext = this;
        }

        [Import]
        public IView PersonView
        {
            get;
            set;
        }

        private int _age = 23;

        [Range(0, 120, ErrorMessage = "年龄值必须在 0-120 之间.")]
        public int Age
        {
            get { return _age; }
            set
            {
                Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "Age" });
                _age = value;
                OnPropertyChanged("Age");
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        /// <summary>
        /// 属性改变事件子类调用
        /// </summary>
        /// <param name="propertyName">属性名</param>
        private void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler propertyChanged = PropertyChanged;
            if (propertyChanged != null)
            {
                propertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
